﻿// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

Shader "Unlit/NewUnlitShader"
{
    Properties
    {
        _Color ("[颜色] color ",color)=(1,1,1,1)
        _MainTex ("[纹理图] Texture", 2D) = "white" {}
        _Gloss("[光泽度] gloss",range(8.0,256))=20
        _Specular("[高光颜色] specular",color)=(1,1,1,1)
    }
    SubShader
    {
        Tags { "LightMode"="ForwardBase" }
        

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"   

            fixed4 _Color;
            sampler2D _MainTex;
            //用于描述纹理的缩放平移数据
            float4 _MainTex_ST;
            float _Gloss;
            fixed4 _Specular;

            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal:NORMAL;
                float4 texcoord : TEXCOORD0;
            };

            struct v2f
            {

                float4 pos : SV_POSITION;
                float3 worldNormal: TEXCOORD0;
                float3 worldPos:TEXCOORD1;
                float2 uv:TEXCOORD2;
            };

            v2f vert (a2v v)
            {
                v2f o;
                //获取裁剪空间下的坐标
                o.pos = UnityObjectToClipPos(v.vertex);
                //获取世界空间下的法线
                o.worldNormal=UnityObjectToWorldNormal(v.normal);
                //世界空间下坐标
                o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
                //uv坐标=模型的纹理坐标(这个词自己编的不严谨)*缩放值+平移值
               o.uv=v.texcoord.xy*_MainTex_ST.xy+_MainTex_ST.zw;

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //得到世界空间的法线方向 归一化即可
                fixed3 worldNormal=normalize(i.worldNormal);
                //获得世界空间的光源方向
                fixed3 worldLightDir=normalize(_WorldSpaceLightPos0.xyz);

                //计算反射率 Tex2D(纹理图,uv坐标) 函数返回纹素值
                fixed3 albedo=tex2D(_MainTex,i.uv)*_Color.rgb;
                //环境光
                fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
                //漫反射
                fixed3 diffuse=_LightColor0.rgb*albedo*saturate(dot(worldLightDir,worldNormal));
                //获取视线方向
                fixed3 viewDir=normalize(_WorldSpaceCameraPos.xyz-i.worldPos.xyz);
                
                fixed3 halfDir=normalize(viewDir+worldLightDir);
                //高光
                fixed3 specular=_LightColor0.rgb*_Specular.rgb*pow(saturate(dot(halfDir,worldNormal)),_Gloss);

                return fixed4(ambient+diffuse+specular,1.0);

            }
            ENDCG
        }

    }
    FallBack "Specular"
}
